const express = require('express')
const router = express.Router()
const Article = require('../../models/article')

//使用分页器查询某一页的数据,
//@GET /vueShow/article/getArt
router.get('/getArt/:current/:pageSize', (req, res) => {

  Article.find({})
    .then(doc => {
      let artNum = doc.length //文章总数
      let current = req.params.current
      let pageSize = req.params.pageSize
      //根据传过来的页码和每页的文章数筛选出来数据
      let arr = doc.filter((item, index) => {
        return index >= (current - 1) * pageSize && index < current * pageSize
      })
      res.send({
        art: arr,
        state: 1,
        msg: '分页查询成功',
        articleNum: artNum
      })
    })
})
//查询一个文章
//@GET /vueShow/article/one
router.get('/one/:id', (req, res) => {
  Article.findOne({ _id: req.params.id })
    .then(doc => {
      console.log(doc)
      
      res.send({ data: doc, state: 1, msg: "查询成功" })
    })
    .catch(err => res.send({ data: err, state: 0, msg: '查询失败' }))
})
//根据分类，查询一个文章
//@GET /vueShow/article/class
router.get('/class/:name', (req, res) => {
  Article.find({ class_name: req.params.name })
    .then(doc => {
      console.log(doc)
      res.send({ data: doc, state: 1, msg: "查询成功" })
    })
    .catch(err => res.send({ data: err, state: 0, msg: '查询失败' }))
})

module.exports = router